﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Modules.MENU.Components;
using ASPNETVN.PORTAL.Components.UI;
using System.Web.UI.HtmlControls;

namespace ASPNETVN.PORTAL.Modules.MENU.Controls
{
    public partial class SlideOutMenu : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string scriptUrl = ResolveClientUrl("~/Modules/ASPNETVN.PORTAL.Modules.MENU/Scripts/SimpleMenu.js");
            const string scriptName = "SlideOutMenuScript";
            Type scriptType = this.GetType();
            ClientScriptManager csm = Page.ClientScript;
            if (!csm.IsClientScriptIncludeRegistered(scriptType, scriptName))
            {
                csm.RegisterClientScriptInclude(scriptType, scriptName, scriptUrl);
            }


            // Left Pane.
            LiteralControl literal = new LiteralControl("");
            literal.EnableViewState = true;
            LeftPane.EnableViewState = true;

            literal.Text += "<div id='dhtmlgoodies_listMenu' style='visibility: hidden;'>";
            literal.Text += "<ul>";

            IList<Item> allItems = Item.SelectCollectionAll(this.ModuleID);
            IList<Item> topItems = allItems.Where(x => x.ParentID == 0).OrderBy(x => x.SortOrder).ToList();


            foreach (Item topItem in topItems)
            {
                literal.Text += string.Format("<li><a href='{0}' target='{2}'>» {1}</a>", topItem.Url, topItem.Title, topItem.Target);
                IList<Item> childItems = allItems.Where(x => x.ParentID == topItem.ID).OrderBy(x => x.SortOrder).ToList();
                if (childItems.Count > 0)
                {
                    literal.Text += "<ul>";
                    foreach (Item childItem in childItems)
                    {
                        literal.Text += string.Format("<li><a href='{0}' target='{2}'>» {1}</a></li>", childItem.Url, childItem.Title, childItem.Target);
                    }
                    literal.Text += "</ul>";
                }
            }
            literal.Text += "</li>";
            literal.Text += "</ul></div>";
            LeftPane.Controls.Add(literal);


            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = "~/Modules/ASPNETVN.PORTAL.Modules.MENU/StyleSheets/SimpleMenu.css";
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(cssLink);
        }
    }
}